﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SoundMatcher.bean_classes;
using System.Data;

namespace SoundMatcher
{
    class SoundMatcher
    {

       public List<int> GetBestFit(double[] input){

           double[] segmentfreDiference = new double[input.Length - 1];

           for (int j = 0; j < (input.Length - 1); j++)//loop to calculate fre difference from segment frequency array
           {
               segmentfreDiference[j] = input[j + 1] - input[j];
           }



         string query = "SELECT  freq_difference, id, title FROM song";
         DataTable songsWithFrequencyDiffs=DB.GetDataTable(query);
         DataTableReader tableReader = songsWithFrequencyDiffs.CreateDataReader();


           Dictionary<int,double> matchingSongsWithAccuracy = new Dictionary<int,double>();
           List<double> accuracyList=new List<double>();
           List<int> matchingSongs = new List<int>();

        while(tableReader.Read()){

           int id= Convert.ToInt32(tableReader["id"]);
           string title = Convert.ToString(tableReader["title"]);
           string freDifferencesString = Convert.ToString(tableReader["freq_difference"]);

           int[] freDifferencesOfOriginal = freDifferencesString.Split(',').Select(n =>int.Parse(n)).ToArray();

//////////////////////////////////////////////////////////////////////////////////////////////////

           int samplesInOriginal = freDifferencesOfOriginal.Length;
           int samplesInSegment = segmentfreDiference.Length;


           int h = 0;
           int x = 0;

           List<double> accuracyListOfaSong = new List<double>();


           for (int i = 0; i <= samplesInOriginal - samplesInSegment; i++)
           {



               x = i;
               // int y = 0;
               for (int j = 0; j < samplesInSegment; j++, x++)
               {
                   int f = Convert.ToInt32(segmentfreDiference[j]);

                   int f1 = (f - 0);        //set the proper range here
                   int f2 = freDifferencesOfOriginal[x];
                   int f3 = (f+ 0);          //set the proper range here

                   if (f1 <= f2)
                   {
                       if (f2 <= f3)
                       {
                           if (f != 0)
                           {
                               h++;
                           }
                           //h++;

                       }
                   }


               }

               double noOfSimilarPoints = Convert.ToDouble(h);
               double accuracy = noOfSimilarPoints / samplesInSegment;

               accuracyListOfaSong.Add(accuracy);



               h = 0;

           }




           double maxAccuracy;
           maxAccuracy = accuracyListOfaSong.Max();                        //taking the maximum accuracy out of the array
           accuracyList.Add(maxAccuracy);
           matchingSongsWithAccuracy.Add(id, maxAccuracy);

           



/////////////////////////////////////////////////////////////////////////////////////////////
               
           //         int range = 0;
           //         int noOfSimilaFre = 0;


           //         for (int y = 0; y < segmentfreDiference.Length;y++ )
           //         {

           //             int f = Convert.ToInt32(segmentfreDiference[y]);

           //             bool success=freDifferencesOfOriginal.Contains(f);
           //             if (success)
           //            {
                           
           //                noOfSimilaFre++;

           //            }
           //            else
           //            {
           //               int k=-1*(range/2);

           //                while (k <= (range/2))
           //                {



           //                    if(k==0){
           //                        k++;
           //                        continue;
           //                    }
           //                    bool freExist = freDifferencesOfOriginal.Contains(f + k);

           //                    if (freExist)
           //                    {

           //                        noOfSimilaFre++;
           //                    }
           //                    k++;
                               
           //                }


           //            }

                       


           //         }


           //         double accuracy = Convert.ToDouble( noOfSimilaFre) /Convert.ToDouble (segmentfreDiference.Length);

                
           // //if(accuracy>.5){

           //     //Song s = new Song();
           //     //s.id = id;
           //     //s.title = title;

           //     matchingSongsWithAccuracy.Add(id, accuracy);
           //     accuracyList.Add(accuracy);

                
           //// }

            
            }



        accuracyList.Sort();
        accuracyList.Reverse();

        foreach (double accuracy in accuracyList)
        {
            foreach (KeyValuePair<int,double> pair in matchingSongsWithAccuracy)
            {
                if (pair.Value == accuracy)
                {
                    if(!matchingSongs.Contains(pair.Key)){
                        matchingSongs.Add(pair.Key);

                    }
                    
                }
            }
        }

           return matchingSongs;
        }

    }
}
